package cf.huzpsb.bcgod;

import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;

public class GroupCMD extends Command {
    public GroupCMD(String name, String permission, String... aliases) {
        super(name, permission, aliases);
    }

    public void execute(CommandSender sender, String[] args) {
        if (!(sender instanceof ProxiedPlayer)) {
            sender.sendMessage("§c后台不推荐使用此命令");
        } else if (((ProxiedPlayer) sender).getServer().getInfo().getName().equalsIgnoreCase(Main.login)) {
            sender.sendMessage("§c登录区域禁止使用!");
            return;
        }
        if (args.length != 3 && args.length != 2) {
            sender.sendMessage("§c参数错误./group <玩家> <组/reset> <时间(秒=86400*天)/-1为永久>");
            return;
        }
        if (args.length == 2 && !args[1].equalsIgnoreCase("reset")) {
            sender.sendMessage("§c参数过少.");
            return;
        }
        int time = 0;
        if (args.length == 3) {
            if (args[1].equalsIgnoreCase("reset")) {
                sender.sendMessage("§c参数过多.");
                return;
            }
            try {
                time = Integer.parseInt(args[2]);
            } catch (Exception e) {
                sender.sendMessage("§c参数错误.");
                return;
            }
        }

        if (Main.groupcfg.contains(args[0] + ".group")) {
            if (args[1].equalsIgnoreCase("reset")) {
                Main.groupcfg.set(args[0] + ".group", null);
                Main.groupcfg.set(args[0] + ".exp", null);
                Main.groupcfg.set(args[0], null);
                sender.sendMessage("§a已经移除本玩家的VIP权限组!");
            } else {
                sender.sendMessage("§c本玩家已经有VIP权限组!");
            }
        } else {
            if (args[1].equalsIgnoreCase("reset")) {
                sender.sendMessage("§c本玩家并没有VIP权限组!");
                return;
            }
            Main.groupcfg.set(args[0] + ".group", args[1]);
            if (time == -1) {
                Main.groupcfg.set(args[0] + ".exp", -1);
                sender.sendMessage("§a设置永久权限组成功!");
            } else {
                Main.groupcfg.set(args[0] + ".exp", System.currentTimeMillis() + 1000 * time);
                sender.sendMessage("§a设置临时权限组成功!");
            }
        }

    }
    //config.set(args[0], (Object)null);
}
